import logging

import requests
from bs4 import BeautifulSoup

from api.reg_login_api import RegLoginApi
from common.read_html_tools import html_util
from config import BASE_HOST


class OpenAccount(object):

    def __init__(self, session):
        self.ses = session

    # 实名认证接口
    def realname(self, realname_v, cardId_v):
        resp = self.ses.post(url=BASE_HOST + "/member/realname/approverealname",
                             data={"realname": realname_v, "card_id": cardId_v}, files={"a": "b"})
        logging.info("实名认证接口响应体数据：{}".format(resp.json()))
        return resp

    # 开户接口
    def open_account(self):
        resp = self.ses.post(url=BASE_HOST + "/trust/trust/register")
        logging.info("开户接口响应体数据：{}".format(resp.json()))
        return resp

    # 第三方开户接口
    def third_open_account(self, url, form_dict):
        resp = self.ses.post(url=url, data=form_dict)
        logging.info("第三方开户开户接口响应体数据：{}".format(resp.text))
        return resp


if __name__ == '__main__':
    ses = requests.session()
    RegLoginApi(ses)
    RegLoginApi(ses).user_login("13100002233", "qq1234")
    OpenAccount(ses)
    OpenAccount(ses).realname("张三", "430802199003075196")
    resp = OpenAccount(ses).open_account()
    print(resp.json())

    # data = resp.json().get("description").get("form")
    # print("========", data)
    # bs = BeautifulSoup(data, "html.parser")
    # url = bs.form.get("action")
    # form_dict = dict()
    # for i in bs.find_all("input"):
    #     key = i.get("name")
    #     value = i.get("value")
    #     form_dict[key] = value

    result = html_util(resp)
    url = result[0]
    form_dict = result[1]
    resp = OpenAccount(ses).third_open_account(url, form_dict)
    print(resp.text)
